﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Xml.Linq;
using System.Web.Routing;
using System.Web.UI;

namespace SimpleRoutingTest
{
	public class Global : System.Web.HttpApplication
	{
		public static void RegisterRoutes( RouteCollection routes )
		{
			// Note: Change the URL to "{controller}.mvc/{action}/{id}" to enable
			//       automatic support on IIS6 and IIS7 classic mode

			// Route existing files (default will shortcut routing if physical file exists)
			// RouteTable.Routes.RouteExistingFiles = true;

			// Add StopRoutingHandler for .axd and .asmx requests
			//routes.Add( new Route( "{resource}.axd/{*pathInfo}", new StopRoutingHandler() ) );
			//routes.Add( new Route( "{service}.asmx/{*pathInfo}", new StopRoutingHandler() ) );

			// Add some named handlers
			routes.Add( "details", new Route( "Details/{category}/{id}", new WebFormRouteHandler( "~/Pages/Details.aspx" ) ) );
			routes.Add( "search", new Route( "Search/{category}", new WebFormRouteHandler( "~/Pages/Search.aspx" ) ) );
			routes.Add( "searchEmpty", new Route( "Search", new WebFormRouteHandler( "~/Pages/Search.aspx" ) ) );
			routes.Add( "noMaster", new Route( "NoMaster/{category}/{id}", new WebFormRouteHandler( "~/Pages/NoMaster.aspx" ) ) );
			routes.Add( "subNoMaster", new Route( "NoMaster/MySub/{category}/{id}", new WebFormRouteHandler( "~/Pages/SubPage1.aspx" ) ) );
			routes.Add( "noMasterEmpty", new Route( "NoMaster", new WebFormRouteHandler( "~/Pages/NoMaster.aspx" ) ) );

			// Add an unnamed handler for simple requests
			routes.Add( "simple", new Route( "{value}", new WebFormRouteHandler( "~/Pages/Simple.aspx" ) ) );
		}

		protected void Application_Start( object sender, EventArgs e )
		{
			RegisterRoutes( RouteTable.Routes );

			SiteMap.SiteMapResolve += SiteMap_SiteMapResolve;
		}

		private SiteMapNode SiteMap_SiteMapResolve( object sender, SiteMapResolveEventArgs e )
		{
			var routable = e.Context.CurrentHandler as IRoutablePage;

			if ( routable != null )
			{
				var rc = routable.Routing.RequestContext;
				var route = rc.RouteData.Route;
				var segments = route.GetVirtualPath( rc, null ).VirtualPath.Split( '/' );
				var path = "~/" + string.Join( "/", segments.Take( segments.Length - rc.RouteData.Values.Count ).ToArray() );

				return SiteMap.Provider.FindSiteMapNodeFromKey( path );
			}

			return null;
		}

		protected void Session_Start( object sender, EventArgs e )
		{

		}

		protected void Application_BeginRequest( object sender, EventArgs e )
		{

		}

		protected void Application_AuthenticateRequest( object sender, EventArgs e )
		{

		}

		protected void Application_Error( object sender, EventArgs e )
		{

		}

		protected void Session_End( object sender, EventArgs e )
		{

		}

		protected void Application_End( object sender, EventArgs e )
		{

		}
	}
}